// SpinButtonPeer.cpp : implementation file
//

#include "stdafx.h"
#include "SpinButtonPeer.h"
#include "SpinButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(SpinButtonPeer, CSpinButtonCtrl)

/////////////////////////////////////////////////////////////////////////////
// SpinButtonPeer

SpinButtonPeer::SpinButtonPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;		
}

SpinButtonPeer::~SpinButtonPeer()
{
}


BEGIN_MESSAGE_MAP(SpinButtonPeer, CSpinButtonCtrl)
	//{{AFX_MSG_MAP(SpinButtonPeer)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(UDN_DELTAPOS, OnDeltapos)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// SpinButtonPeer message handlers

int SpinButtonPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CSpinButtonCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
	return 0;
}

void SpinButtonPeer::OnDeltapos(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here

	SpinButton* comp = (SpinButton*)m_comp;
	if (pNMUpDown->iDelta == 1)
	{	
		AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::BLOCK_INCREMENT, AWTAdjustmentEvent::BLOCK_INCREMENT, 0);
		comp->processAdjustmentEvent(&e);			
	}
	else
	{
		AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::BLOCK_DECREMENT, AWTAdjustmentEvent::BLOCK_DECREMENT, 0);
		comp->processAdjustmentEvent(&e);			
	}
	*pResult = 0;
}
